package com.systemframework.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systemframework.annotation.LicenseAnnotation;
import com.systemframework.annotation.LogAnnotation;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.constants.Constants;
import com.systemframework.enums.BusinessType;
import com.systemframework.sys.dto.request.EmailAccountRequestDTO;
import com.systemframework.sys.dto.response.EmailAccountResponseDTO;
import com.systemframework.sys.entity.EmailAccount;
import com.systemframework.sys.service.EmailAccountService;
import com.systemframework.util.MD5;
import com.systemframework.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @date 2023-04-04 17:37
 */
@RestController
@RequestMapping(value = "/emailAccount")
public class EmailAccountController implements BaseCommonInterface<EmailAccountRequestDTO, EmailAccountResponseDTO> {

    private EmailAccountService emailAccountService;



    @Autowired
    public void setEmailAccountService(EmailAccountService emailAccountService) {
        this.emailAccountService = emailAccountService;
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.INSERT,name = "邮箱信息")
    @Override
    public Result<String> save(@Valid EmailAccountRequestDTO emailAccountRequestDTO) {
        EmailAccount emailAccount = Convert.convert(EmailAccount.class, emailAccountRequestDTO);
        emailAccount.setPassword(MD5.aesEncrypt(emailAccount.getEmail(),emailAccount.getPassword()));
        emailAccountService.save(emailAccount);
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "邮箱信息")
    @Override
    public Result<String> update(EmailAccountRequestDTO emailAccountRequestDTO) {
        EmailAccount emailAccount = Convert.convert(EmailAccount.class, emailAccountRequestDTO);
        emailAccountService.updateById(emailAccount);
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.DELETE,name = "邮箱信息")
    @Override
    public Result<String> del(Integer id) {
        emailAccountService.removeById(id);
        return Result.successResult();
    }


    @GetMapping(value = "/findLists")
    @LicenseAnnotation
    public Result<List<EmailAccountResponseDTO>>  findLists(){
        LambdaQueryWrapper<EmailAccount> eq = Wrappers.lambdaQuery(EmailAccount.class).eq(EmailAccount::getEnabledSsl, Constants.ONE);

      List<EmailAccount> list = emailAccountService.list(eq);
        return Result.successResult(Convert.convert(new TypeReference<List<EmailAccountResponseDTO>>() {},list));
    }

    @LicenseAnnotation
    @Override
    public Result<Page<EmailAccountResponseDTO>> lists(EmailAccountRequestDTO emailAccountRequestDTO) {
        Page<EmailAccountResponseDTO> page=emailAccountService.lists(emailAccountRequestDTO);
        return Result.successResult(page);
    }

    @LicenseAnnotation
    @Override
    public Result<EmailAccountResponseDTO> findById(Integer id) {
        EmailAccountResponseDTO emailAccountResponseDTO = Convert.convert(EmailAccountResponseDTO.class, emailAccountService.getById(id));
        return Result.successResult(emailAccountResponseDTO);
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "邮箱信息")
    @Override
    public Result<String> batchDel(List<EmailAccountResponseDTO> lists) {
        List<Integer> ids = lists.stream().map(EmailAccountResponseDTO::getId).collect(Collectors.toList());
        if(ids.size()>0 && CollUtil.isNotEmpty(ids)){
            emailAccountService.removeByIds(ids);
        }
        return Result.successResult();
    }


}
